package org.gwu.seuss.server.command;

import org.gwu.seuss.communication.request.Actions;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.springframework.util.Assert;

public class UpdateUsernameCommand extends UserCommandBase implements Command {

	public UpdateUsernameCommand(ClientRequest req) {
		super(req);
	}

	@Override
	public ServerResponse execute() throws Exception {
		String username = req.getUsername();
		//The new username field will be populated in the UserTO.
		String newUsername = req.getUser().getUsername();
		
		Assert.hasText(username, "Username is not provided.");
		Assert.hasText(newUsername, "New username is not provided.");
		
		userService.actionAllowed(username, Actions.UPDATE_USERNAME);
		log.info("User " + username + "requested to update username to " + newUsername);
		
		int result = userService.updateUserEmail(username, newUsername);
		
		return getServerResponse(result, "Username " + username + " has been updated to " + newUsername);
	}

}
